/**
 * @param {string} sentence
 * @return {number}
 */
 var countValidWords = function(sentence) {
  sentence = sentence.trim().replace(/\s+/g, ' ')
  const arr = sentence.split(' ')
  const a = new Set(['!', '.', ',', ' '])
  let ans = 0
  const check = (x) => x.charCodeAt(0) >= 97 && x.charCodeAt(0) <= 122
  for(const s of arr){
      let flag = true
      let cnt1 = 0, cnt2 = 0
      for(let i = 0; i<s.length; i++){
          const ch = s[i]
          if(a.has(ch)){
              cnt1++
          } else if(ch === '-'){
              if(i === 0 || i === s.length-1 || !check(s[i-1]) || !check(s[i+1])){
                  flag = false
              }
              cnt2++
          }else if(!check(ch)) flag = false
      }
      if(flag && cnt1 < 2  && cnt2 < 2){
          if(cnt1 > 0 && a.has(s[s.length-1])){
              ans++
          }else if(cnt1 === 0){
              ans++
          }
      }
  }
  return ans
};